package rsj.api.orgp;

import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.TableNode;
import common.util.Assert;
import common.util.BoxUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.user.BaseLink;
import rsj.common.CalculateEnum;
import rsj.context.ApplicationContextUtil;
import rsj.service.OrgPService2;
import rsj.sql.user.UserDao;
import rsj.table.ComStatus;

@RestController
public class GetOrgProdList extends BaseLink {

    private static final OrgPService2 orgPService = OrgPService2.getInstance();

    @RequestMapping(value = "rsj_operative_prodList")
    public Box executeLink(Box in) {
        MapNode data = in.getMapNode("data");
        MapNode user = in.getMapNode("$user");
        String userId = user.getValue("User_Id");
        Assert.isNotBlank( userId, "用户id不能为空！");
        UserDao userDao = ApplicationContextUtil.getBean(UserDao.class);
        String orgId = userDao.queryOrgIdByUser(userId).getValue("User_Org_Id");
        Assert.isNotBlank( orgId, "Org_Id不能为空！");
        String billMethod = data.getValue("Bill_Method");
        Assert.isTrue(CalculateEnum.BilByBar.getCode().equals(billMethod)
                || CalculateEnum.BilByStandard.getCode().equals(billMethod)
                ||CalculateEnum.BilBySubscribe.getCode().equals(billMethod)
                || "0".equals(billMethod)
                , "计费方式只支持 1包量计费，2按条计费，3按订阅量计费，0全部");

        MapNode map = new MapNode();

        if("0".equals(billMethod)){// 全量
            TableNode byStandNode = orgPService.getOrgPList(orgId, ComStatus.BillStatus.ByStand);
            TableNode byBarNode = orgPService.getOrgPList(orgId, ComStatus.BillStatus.ByBar);
            TableNode bySubscribeNode = orgPService.getOrgPList(orgId, ComStatus.BillStatus.BySubscribe);

            map.putNode("ByBar", byBarNode.toJson());
            map.putNode("ByStand", byStandNode.toJson());
            map.putNode("BySubscribe", bySubscribeNode.toJson());
        } else {// 单个
            TableNode mapNode = orgPService.getOrgPList(orgId, billMethod);

            String key;
            if(ComStatus.BillStatus.ByStand.equals(billMethod)){
                key = "ByStand";
            }else if(ComStatus.BillStatus.ByBar.equals(billMethod)){
                key = "ByBar";
            }else {
                key = "BySubscribe";
            }

//            String key = ComStatus.BillStatus.ByStand.equals(billMethod) ? "ByStand" : "ByBar";
            map.putNode(key, mapNode.toJson());
        }

        return BoxUtil.successBoxToData(in, "获取当前机构合同列表成功！", map);
    }
}
